import 'dart:math';

import 'package:flutter/material.dart';
import 'package:x3_charts_core/chart_tools/chart_tools.dart';
import 'package:x3_charts_core/model/cob_web_chart_data_model.dart';

///**
///* @author hrongyong
///* @date   2/4/21
///* @email hrongyong@gmail.com
///* Description:
///*/

class CobwebChartPainter extends CustomPainter {
  ///多边形几个边
  int sideNum = 5;

  ///默认几层多边形,默认5层，每层20
  int layerNum = 5;

  ///组件的中心
  double viewCenterX = 0.0;
  double viewCenterY = 0.0;

  ///最大半径
  double maxRadius = 0.0;

  ///背景画笔
  Paint bgPaint;

  ///背景路径
  Path bgPath;

  ///遮罩画笔
  Paint layerPaint;

  ///遮罩画笔的填充颜色
  Color fillColor;

  ///遮罩层的描边颜色
  Color strokeColor;

  ///文字
  final TextPainter _textPainter = TextPainter(textDirection: TextDirection.ltr);
  final TextPainter _textSubPainter = TextPainter(textDirection: TextDirection.ltr);

  ///圆的度数
  final double CIRCLE_ANGLE = 360;

  List<CobwebChartDataModel> cobwebList;

  CobwebChartPainter({@required this.cobwebList, @required this.fillColor, @required this.strokeColor, this.sideNum = 5}) {
    bgPaint = Paint()
      ..color = Colors.black26.withAlpha(10)
      ..isAntiAlias = true
      ..style = PaintingStyle.stroke;
    bgPath = Path();

    layerPaint = Paint()
      ..color = fillColor
      ..isAntiAlias = true
      ..style = PaintingStyle.fill;
  }

  @override
  void paint(Canvas canvas, Size size) {
    ///获取中心
    viewCenterX = size.width / 2;
    viewCenterY = size.height / 2;

    ///得到最大半径
    maxRadius = viewCenterX > viewCenterY ? viewCenterY : viewCenterX;

    ///将组件划分画图区域和文字区域 => 画图区域:文字区域 占比为 6:4
    maxRadius = maxRadius * 0.6;
    canvas.translate(size.width / 2, size.height / 2);
    canvas.rotate(-pi / 2);

    ///背景雷达图
    drawPolygon(canvas);

    ///遮罩层
    drawMaskLayer(canvas);

    ///绘制文字
    drawText(canvas);
  }

  double eachRadius;
  double eachAngle;

  ///背景雷达图
  void drawPolygon(Canvas canvas) {
    ///每个角的度数
    eachAngle = CIRCLE_ANGLE / sideNum;

    ///找好所有的顶点，连接起来即可
    for (int i = 0; i < layerNum; i++) {
      // 根据层级循环绘画
      ///清理路径
      bgPath.reset();

      ///每层的半径
      eachRadius = maxRadius / layerNum * (i + 1);

      for (int j = 0; j < sideNum + 1; j++) {
        // 确定每层后 根据边开始计算路径
        if (j == 0) {
          ///移动路径起点到指定位置
          bgPath.moveTo(0 + eachRadius, 0);
        } else {
          double x = eachRadius * cos(ChartTools.degToRad(eachAngle * j));
          double y = eachRadius * sin(ChartTools.degToRad(eachAngle * j));
          bgPath.lineTo(x, y);
        }
      }
      //闭合路径
      bgPath.close();

      ///绘制圈圈
      canvas.drawPath(bgPath, bgPaint);
    }

    ///绘制半径线
    drawLineLinkPoint(canvas, eachAngle, eachRadius);
  }

  ///根据半径及角度绘制半径线
  void drawLineLinkPoint(Canvas canvas, double eachAngle, double eachRadius) {
    bgPath.reset();
    for (int i = 0; i < sideNum; i++) {
      bgPath.moveTo(0, 0);
      double x = eachRadius * cos(ChartTools.degToRad(eachAngle * i));
      double y = eachRadius * sin(ChartTools.degToRad(eachAngle * i));
      bgPath.lineTo(x, y);
      bgPath.close();
      canvas.drawPath(bgPath, bgPaint);
    }
  }

  ///绘制遮罩层
  void drawMaskLayer(Canvas canvas) {
    bgPath.reset();
    if (cobwebList.length == 0) return;
    for (int i = 0; i < sideNum; i++) {
      double mRandomInt = cobwebList[i].value;
      double x = maxRadius * cos(ChartTools.degToRad(eachAngle * i)) * mRandomInt;
      double y = maxRadius * sin(ChartTools.degToRad(eachAngle * i)) * mRandomInt;
      if (i == 0) {
        bgPath.moveTo(x, 0);
      } else {
        bgPath.lineTo(x, y);
      }
    }
    bgPath.close();
    canvas.drawPath(
        bgPath,
        Paint()
          ..style = PaintingStyle.stroke
          ..isAntiAlias = true
          ..strokeWidth = 1
          ..color = strokeColor);
    canvas.drawPath(bgPath, layerPaint);
  }

  ///绘制文字
  void drawText(Canvas canvas) {
    if (cobwebList.length == 0) return;

    ///由于雷达图是旋转了90度的，所以文字也要旋转90度
    canvas.rotate(pi / 2);
    for (int i = 0; i < sideNum; i++) {
      double x = maxRadius * cos(ChartTools.degToRad(eachAngle * i - 90));
      double y = maxRadius * sin(ChartTools.degToRad(eachAngle * i - 90));

      _drawAxisText(canvas, title: cobwebList[i].name, subTitle: cobwebList[i].subTitle, offset: Offset(x, y), index: i);
    }
  }

  ///标题和副标题一起绘制
  void _drawAxisText(
    Canvas canvas, {
    String title,
    String subTitle,
    Offset offset = Offset.zero,
    int index,
  }) {
    ///绘制标题
    TextSpan text = TextSpan(text: title, style: TextStyle(fontSize: 8, color: Colors.black));
    _textPainter.text = text;
    _textPainter.layout(); // 进行布局
    Size textSize = _textPainter.size;

    ///绘制副标题
    TextSpan subText = TextSpan(text: subTitle, style: TextStyle(fontSize: 8, color: Colors.black));
    _textSubPainter.text = subText;
    _textSubPainter.layout(); // 进行布局
    Size subTextSize = _textSubPainter.size;

    Offset textOffsetPos = ChartTools.roundStructureTextOffset(offset: offset, textSize: textSize, subTextSize: subTextSize)[0];
    _textPainter.paint(canvas, textOffsetPos);

    Offset subTextOffsetPos = ChartTools.roundStructureTextOffset(offset: offset, textSize: textSize, subTextSize: subTextSize)[1];
    _textSubPainter.paint(canvas, subTextOffsetPos);
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) => false;
}
